import React, { forwardRef } from 'react';
import * as AlertPrimitive from '@radix-ui/react-alert-dialog';

// Alert组件
const Alert = ({ className, children, ...props }: React.HTMLAttributes<HTMLDivElement>) => (
  <div
    className={`relative w-full rounded-lg border px-4 py-3 text-sm ${className}`}
    role="alert"
    {...props}
  >
    {children}
  </div>
);

// AlertTitle组件
const AlertTitle = ({ className, children, ...props }: React.HTMLAttributes<HTMLDivElement>) => (
  <div className={`font-medium ${className}`} {...props}>
    {children}
  </div>
);

// AlertDescription组件
const AlertDescription = ({ className, children, ...props }: React.HTMLAttributes<HTMLParagraphElement>) => (
  <p className={`[&+&]:mt-1 ${className}`} {...props}>
    {children}
  </p>
);

// AlertClose组件
const AlertClose = forwardRef<HTMLButtonElement, React.ButtonHTMLAttributes<HTMLButtonElement>>(({ className, ...props }, ref) => (
  <button
    ref={ref}
    type="button"
    className={`absolute right-4 top-4 rounded-full p-1 text-muted-foreground transition-colors hover:text-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 ${className}`}
    {...props}
  >
    <svg width="14" height="14" viewBox="0 0 14 14" fill="none">
      <path d="M11 3L3 11" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
      <path d="M3 3L11 11" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  </button>
));

// AlertDialog组件
const AlertDialog = AlertPrimitive.Root;

// AlertDialogTrigger组件
const AlertDialogTrigger = AlertPrimitive.Trigger;

// AlertDialogPortal组件
const AlertDialogPortal = AlertPrimitive.Portal;

// AlertDialogOverlay组件
const AlertDialogOverlay = forwardRef<HTMLDivElement, AlertPrimitive.AlertDialogOverlayProps>(({ className, ...props }, ref) => (
  <AlertPrimitive.Overlay
    ref={ref}
    className={`fixed inset-0 z-50 bg-black/50 backdrop-blur-sm data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 ${className}`}
    {...props}
  />
));

// AlertDialogContent组件
const AlertDialogContent = forwardRef<HTMLDivElement, AlertPrimitive.AlertDialogContentProps>(({ className, ...props }, ref) => (
  <AlertPrimitive.Content
    ref={ref}
    className={`fixed left-1/2 top-1/2 z-50 w-full max-w-md -translate-x-1/2 -translate-y-1/2 rounded-lg border bg-background p-6 shadow-lg animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 sm:max-w-lg ${className}`}
    {...props}
  />
));

// AlertDialogHeader组件
const AlertDialogHeader = ({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) => (
  <div className={`flex flex-col space-y-2 text-center sm:text-left ${className}`} {...props} />
);

// AlertDialogTitle组件
const AlertDialogTitle = forwardRef<HTMLHeadingElement, AlertPrimitive.AlertDialogTitleProps>(({ className, ...props }, ref) => (
  <AlertPrimitive.Title
    ref={ref}
    className={`text-xl font-semibold leading-none tracking-tight ${className}`}
    {...props}
  />
));

// AlertDialogDescription组件
const AlertDialogDescription = forwardRef<HTMLParagraphElement, AlertPrimitive.AlertDialogDescriptionProps>(({ className, ...props }, ref) => (
  <AlertPrimitive.Description
    ref={ref}
    className={`text-sm text-muted-foreground ${className}`}
    {...props}
  />
));

// AlertDialogFooter组件
const AlertDialogFooter = ({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) => (
  <div className={`flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2 ${className}`} {...props} />
);

// AlertDialogAction组件
const AlertDialogAction = forwardRef<HTMLButtonElement, AlertPrimitive.AlertDialogActionProps>(({ className, ...props }, ref) => (
  <AlertPrimitive.Action
    ref={ref}
    className={`inline-flex h-10 items-center justify-center rounded-md bg-primary px-4 py-2 text-sm font-medium text-primary-foreground shadow-sm transition-colors hover:bg-primary/90 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 ${className}`}
    {...props}
  />
));

// AlertDialogCancel组件
const AlertDialogCancel = forwardRef<HTMLButtonElement, AlertPrimitive.AlertDialogCancelProps>(({ className, ...props }, ref) => (
  <AlertPrimitive.Cancel
    ref={ref}
    className={`inline-flex h-10 items-center justify-center rounded-md bg-muted px-4 py-2 text-sm font-medium text-muted-foreground shadow-sm transition-colors hover:bg-muted/80 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 ${className}`}
    {...props}
  />
));

// 导出所有组件
export {
  Alert,
  AlertClose,
  AlertDescription,
  AlertTitle,
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogOverlay,
  AlertDialogPortal,
  AlertDialogTitle,
  AlertDialogTrigger,
};